#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    struct Sort
    {
        bool operator()(const pair<int, string>& p, const pair<int, string>& q)
        {
            if (p.first < q.first)
                return true;
            else if (p.first == q.first)
                return p.second > q.second;
            else
                return false;
        }
    };

    vector<string> topKFrequent(vector<string>& words, int k) {
        unordered_map<string, int> hash;
        for (auto& e : words)
        {
            hash[e]++;
        }
        priority_queue<pair<int, string>, vector<pair<int, string>>, Sort> topK;
        for (auto& e : hash)
        {
            topK.push(make_pair(e.second, e.first));
        }
        vector<string> ret;
        while (k--)
        {
            ret.push_back(topK.top().second);
            topK.pop();
        }

        return ret;
    }
};